#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project  : ecshop
# @File     : backend_add_discount.py
# @Author   : WanLiWei
# @Time     : 2021/7/3 1:45
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("http://192.168.4.138/ecshop/admin/privilege.php?act=login")
driver.find_element(By.NAME, "username").send_keys("admin")
driver.find_element(By.NAME, "password").send_keys("wlw12345678")
driver.find_element(By.CLASS_NAME, "button").click()

# 进入id为menu-frame的frame
driver.switch_to.frame('menu-frame')
# 点击促销管理
driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[2]').click()
# 点击优惠活动
driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[2]/ul/li[8]/a').click()
# 退出frame
driver.switch_to.default_content()

# 进入id为main-frame的frame
driver.switch_to.frame('main-frame')

# 点击优惠活动
driver.find_element_by_xpath('/html/body/h1/span[1]/a').click()
# 优惠活动名称
driver.find_element(By.ID,'act_name').send_keys("双11啦")
# 优惠开始时间
# 删除元素中属性的方法--日期处理
# 开始日期
js = "document.getElementById('start_time').removeAttribute('ReadOnly')"
driver.execute_script(js)
driver.find_element_by_id("start_time").clear()
driver.find_element_by_id("start_time").send_keys("2021-11-11")

# 优惠结束时间
js = "document.getElementById('end_time').removeAttribute('ReadOnly')"
driver.execute_script(js)
driver.find_element_by_id("end_time").clear()
driver.find_element_by_id("end_time").send_keys("2021-11-22")
# 享受优惠的会员等级
driver.find_element_by_xpath('/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[2]').click()
# 优惠范围
driver.find_element_by_xpath('/html/body/div[1]/form/table/tbody/tr[5]/td[2]/select/option[1]').click()
# 金额下限
driver.find_element(By.ID,'min_amount').clear()
driver.find_element(By.ID,'min_amount').send_keys(100)
# 金额上限
driver.find_element(By.ID,'max_amount').clear()
driver.find_element(By.ID,'max_amount').send_keys(1000)
# 优惠方式
sleep(2)
driver.find_element_by_xpath('//select[@id="act_type"]/option[2]').click()
driver.find_element_by_xpath('//input[@id="act_type_ext"]').clear()
driver.find_element_by_xpath('//input[@id="act_type_ext"]').send_keys(50)
sleep(3)

# 确定按钮
# driver.find_element(By.CLASS_NAME,'button').click()
driver.find_element_by_xpath('/html/body/div[4]/table/thead/tr[2]/td[3]/div').click()
driver.find_element_by_xpath('/html/body/div[4]/table/thead/tr[2]/td[3]/div').click()
driver.find_element(By.CLASS_NAME,'button').click()

# 退出frame
driver.switch_to.default_content()
